<template>
  <el-dialog
    width="600px"
    :title="$t('批量设置分组')"
    destroy-on-close
    append-to-body
    :visible.sync="show"
    :before-close="cancel">
    <el-table
      @selection-change="(rows) => (tableCheck = rows)"
      :height="calTableHeight() - 200"
      :data="tableData"
      ref="multipleTables">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="groupName"
        :label="$t('分组名')"
        :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <div slot="footer" class="dialog-footer">
      <el-button @click="handleSubmit" type="primary">
        {{ $t('确定') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getCloudGroup } from '@/api/distribution/sku/assemblyList'
import { setSkuCloudGroupBatch } from '@/api/platform/cloudInvisibleGroupSku'
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  props: {
    /**
     * formData
     */
    formData: {
      type: Object,
      default: {},
    },
    /**
     * query
     */
    query: {
      type: Array,
      default: () => [],
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      tableData: [],
      tableCheck: [],
    }
  },
  computed: {},
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.tableCheck = []
        this.getList()
      }
    },
  },

  methods: {
    getList() {
      getCloudGroup().then((res) => {
        this.tableData = res.data
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
    handleSubmit() {
      const data = {
        groupIdList: this.tableCheck.map((i) => i.id),
        skuIdList: this.query.map((i) => i.skuId),
      }

      setSkuCloudGroupBatch(data).then((res) => {
        this.$message({ type: 'success', message: res.msg })
        this.$emit('getList')
        this.cancel()
      })
    },
  },
}
</script>
<style scoped lang="scss"></style>
